//
//  NoteCommentBox.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/22.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "NoteCommentBox.h"
#import "PlaceholderTextView.h"

#define  HEIGHT_TEXTVIEW   36
#define  BOXTEXTViewSPACE 6.5



@interface NoteCommentBox () <UITextViewDelegate> {
    CGFloat keyboardY;
    CGFloat keyboardH;//消失的时候直接置为0
    CGFloat totalBoxH;//统计总高度
    NoteCommentMode *comModel;
}

@property (weak, nonatomic) IBOutlet UIButton *likeBtn;

@property (weak, nonatomic) IBOutlet PlaceholderTextView *commentTextview;

@property (weak, nonatomic) IBOutlet UIButton *takeBtn;


@end

@implementation NoteCommentBox

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (id)init {
    if (self = [super init]) {
        
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self addNotification];
}

#pragma mark - public
- (BOOL)boxisFirstResponder {
    return [self.commentTextview isFirstResponder];
}

- (void)dismissFirstResponder {
    [self.commentTextview resignFirstResponder];
}

- (void)reverFocus:(NoteCommentMode *)comMode {
    comModel = comMode;
    if ([self.commentTextview isFirstResponder]) {
        [self.commentTextview resignFirstResponder];
    } else {
        self.commentTextview.placeholder =  NSStringFormat(@"@%@",comMode.user.userName);
        [self.commentTextview becomeFirstResponder];
    }
}

- (CGFloat )initHeight {
    return HEIGHT_TEXTVIEW + BOXTEXTViewSPACE *2;
}

#pragma mark--添加通知---
-(void)addNotification{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
}

#pragma mark---textview--代理方法---
-(void)textViewDidBeginEditing:(UITextView *)textView{
    [self changeFrame:ceilf([textView sizeThatFits:textView.frame.size].height)];
}
-(void)textViewDidChange:(UITextView *)textView{
    NSLog(@"textChange");
    self.commentTextview.placeholder = [self plachorder];
    [self changeFrame:ceilf([textView sizeThatFits:textView.frame.size].height)];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if ([text isEqualToString:@"\n"]) {
        if (self.commentTextview.text.length >0) {
            if (self.sendComBlock) {
                if (self.commentTextview.text.length > TEXTLENG) {
                    !self.overflowBlock?:self.overflowBlock();

                } else {
                    self.sendComBlock(self.commentTextview.text);
                    self.commentTextview.text = @"";
                }
            }
        }
        [self.commentTextview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(HEIGHT_TEXTVIEW));
        }];
        [self textViewDidChange:self.commentTextview];
        self.commentTextview.placeholder = [self plachorder];
        return NO;
    }
    return YES;
}


- (void)changeFrame:(CGFloat)height{
    CGFloat maxH = 0;
    if (self.maxVisibleLine) {
        maxH = ceil(self.commentTextview.font.lineHeight * (self.maxVisibleLine - 1) + self.commentTextview.textContainerInset.top + self.commentTextview.textContainerInset.bottom);
    }
    self.commentTextview.scrollEnabled = height >maxH && maxH >0;
    if (self.commentTextview.scrollEnabled) {
        height = 5+maxH;
    }else{
        height = height;
    }
    CGFloat textviewH = height;
    
    CGFloat totalH = 0;
    
    totalH = textviewH + BOXTEXTViewSPACE *2;
    

    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(totalH);
        make.bottom.equalTo(self.superview).offset(-keyboardH);
    }];
    
    [self.commentTextview scrollRangeToVisible:NSMakeRange(0, self.commentTextview.text.length)];
    if (totalBoxH != totalH) {
        Log(@"我调了");
        if (self.changeFrameBlock && keyboardH > 0) {
            self.changeFrameBlock(kScreenHeight - totalH - keyboardH -kNavAndStatusBarHeight);
        }
    }
    
    totalBoxH = totalH;
}


#pragma mark - 通知
-(void)keyboardWillChangeFrame:(NSNotification *)notification{
    
    NSDictionary *userInfo = notification.userInfo;
    // 动画的持续时间
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    // 键盘的frame
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        NSLog(@"%@",NSStringFromCGRect(keyboardF));
    keyboardY = keyboardF.origin.y;
    BOOL isBeyondScreen = keyboardY >= kScreenHeight;
    keyboardH =  isBeyondScreen ? 0 : keyboardF.size.height;
    if (isBeyondScreen) {
        totalBoxH = 0;
    }
    // 执行动画
    if (!_isDisappear) {
        
        [UIView animateWithDuration:duration animations:^{
            // 工具条的Y值 == 键盘的Y值 - 工具条的高度
            if (keyboardY >= kScreenHeight) {
                [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_equalTo(self.superview.mas_bottom).offset(0);
                }];
            }else
            {
                [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.equalTo(self.superview).offset(-keyboardH);
                }];
            }
        }];
    }
}

-(void)keyboardDidChangeFrame:(NSNotification *)notification{
    
    NSDictionary *userInfo = notification.userInfo;
    // 动画的持续时间
    //    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // 键盘的frame
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //    NSLog(@"%@",NSStringFromCGRect(keyboardF));
    keyboardY = keyboardF.origin.y;
    keyboardH =  keyboardY >= kScreenHeight ? 0 : keyboardF.size.height;
  
    
    // 工具条的Y值 == 键盘的Y值 - 工具条的高度
    if (_isDisappear) {
        if (keyboardY >= kScreenHeight) {
            [self mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.superview).offset(0);
            }];
        }else
        {
            [self mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.superview).offset(-keyboardH);
            }];
        }
        //        NSLog(@"%f",self.y);
    }
}


#pragma mark - private 
- (NSString *)plachorder {
    if (self.commentTextview.text.length > 0) {
        return @"";
    }
    return NSStringFormat(@"@%@",comModel.user.userName);
}


@end
